package mall.kotlin.com.myapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * USER: zjw
 * DATA: 2018/11/14 0014
 * TIME: 下午 3:42
 */
public class HorizontalView extends ViewGroup {
    private int lastX; //最后的x
    private int lastY; //最后的y
    private int currentIndex = 0; //当前元素的索引
    private int childWidth = 0;   //元素的宽度
    private Scroller mScroller;   //滚动工具
    private VelocityTracker mTracker;  //快速滑动的工具
    private int lastInterceptX = 0;
    private int lastInterceptY = 0;


    public HorizontalView(Context context) {
        super(context);
        init(context);
    }

    public HorizontalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HorizontalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mScroller = new Scroller(context);
        mTracker = VelocityTracker.obtain();
    }

    //处理滑动冲突
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept = false;
        int x = (int) getX();
        int y = (int) getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                intercept = false;
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int dex = x - lastInterceptX;
                int dey = y - lastInterceptY;
                if (Math.abs(dex) - Math.abs(dey) > 0) {
                    intercept = true;
                } else {
                    intercept = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        lastX = x;
        lastY = y;
        lastInterceptX = x;
        lastInterceptY = y;

        return intercept;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //处理wrap_content的时候
        if (getChildCount() == 0) {
            setMeasuredDimension(0, 0);
        } else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            View childAt = getChildAt(0);
            int width = childAt.getMeasuredWidth();
            int height = childAt.getMeasuredHeight();
            setMeasuredDimension(width * getChildCount(), height);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            int measuredWidth = getChildAt(0).getMeasuredWidth();
            setMeasuredDimension(measuredWidth * getChildCount(), heightSize);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            int height = getChildAt(0).getMeasuredHeight();
            setMeasuredDimension(widthSize, height);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int left = 0;
        View child;
        for (int i = 0; i < childCount - 1; i++) {
            child = getChildAt(i);
            //子元素不为gone
            if (child.getVisibility() != View.GONE) {
                int measuredWidth = child.getMeasuredWidth();
                childWidth = measuredWidth;
                //left是累计,高度为改元素的高度
                child.layout(left, 0, left + measuredWidth, child.getMeasuredHeight());
                left += measuredWidth;
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) getX();
        int y = (int) getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int dex = x - lastX;
                scrollBy(-dex, 0);
                break;
            case MotionEvent.ACTION_UP:
                int desx = getScrollX() - currentIndex * childWidth;
                if (Math.abs(desx) > childWidth / 2) {
                    if (desx > 0) {
                        currentIndex++;
                    } else {
                        currentIndex--;
                    }

                } else {
                    mTracker.computeCurrentVelocity(1000);
                    float xVelocity = mTracker.getXVelocity();
                    if (Math.abs(xVelocity) > 50) {
                        currentIndex--;
                    } else {
                        currentIndex++;
                    }
                }
                currentIndex = currentIndex < 0 ? 0 : currentIndex > getChildCount() - 1 ? getChildCount() - 1 : currentIndex;
                smoothScrollTo(currentIndex * childWidth, 0);
                mTracker.clear();
                break;
        }
        lastX = x;
        lastY = y;
        return super.onTouchEvent(event);
    }

    //view的ondraw()中调用这个方法

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    private void smoothScrollTo(int i, int i1) {
        mScroller.startScroll(getScrollX(), getScrollY(), i - getScrollX(), i1 - getScrollY(), 1000);
        invalidate();
    }
}
